<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var distributeCookies = function (cookies, k) {
      const n = cookies.length;
      const sum = Array(1 << n).fill(0);
      for (let i = 0; i < n; i++) {
        for (let j = 0, bit = 1 << i; j < bit; j++) {
          sum[bit | j] = sum[j] + cookies[i];
        }
      }

      const f = sum.slice();
      for (let i = 1; i < k; i++) {
        for (let j = (1 << n) - 1; j; j--) {
          for (let s = j; s; s = (s - 1) & j) {
            f[j] = Math.min(f[j], Math.max(sum[j ^ s], f[s]));
          }
        }
      }
      return f[(1 << n) - 1];
    };
    console.log(distributeCookies([8, 15, 10, 20, 8], 2));
  </script>
</body>

</html>